package com.keke.beauty.auth.activity.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.keke.beauty.auth.activity.service.TabActivityService;
import com.keke.beauty.auth.activityInvolvement.entity.TabActivityInvolvement;
import com.keke.beauty.auth.activityInvolvement.service.TabActivityInvolvementService;
import com.keke.beauty.sys.wxUser.service.TabWxUserService;
import com.keke.beauty.utils.pojo.Result;
import com.keke.beauty.utils.tools.ResultUtil;

/**
 * 活动管理控制层
 *
 * @author
 * @Data 2019年02月22日 15:37:34
 */
@RestController
@RequestMapping("/wx/activity")
public class WxActivityController {

    @Autowired
    private TabActivityService service;

    @Autowired
    private TabWxUserService wxUserService;

    @Autowired
    private TabActivityInvolvementService activityInvolvementService;

    /**
     * 查看活动管理
     *
     * @param id
     * @return
     * @author
     * @Data 2019年02月22日 15:37:34
     */
    @GetMapping("/{id}/{openId}")
    public Result findById(@PathVariable int id, @PathVariable String openId, String referralsOpenId) {
        Map<String, Object> map = new HashMap<String, Object>(2);
        activityInvolvementService.salute(openId, id, referralsOpenId, null, null);
        map.put("activity", service.findByPrimaryKey(id));
        map.put("userInfo", wxUserService.findByPrimaryKey(openId));
        return ResultUtil.success(map);
    }


    @GetMapping("integralsInfo/{id}/{openId}")
    public Result findAttrById(@PathVariable int id, @PathVariable String openId, String referralsOpenId) {
        TabActivityInvolvement activityInvolvement = new TabActivityInvolvement();
        activityInvolvement.setActivityId(id);
        activityInvolvement.setOpenid(openId);
        activityInvolvement.setCreateTime(new Date());
        Map<String, Object> attrMap = activityInvolvementService.selectByOpenIdAndActivityId(activityInvolvement);
        if (null == attrMap) {
            activityInvolvement.setReferrals(referralsOpenId);
            activityInvolvementService.insert(activityInvolvement);
            attrMap = activityInvolvementService.selectByOpenIdAndActivityId(activityInvolvement);
        }
        return ResultUtil.success(attrMap);
    }

}